﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;

namespace KC.Business.Blog
{

	public static class Encoding
	{
		/// <summary>Given input from the admin edit textbox, encodes and prepares a blog post body to be saved into SQL</summary>
		/// <param name="input">input to encode</param>
		/// <returns>Encoded body</returns>
		public static string EncodeForRendering(string input)
		{
			if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input");
			input = input
				.Replace("[[", "<")
				.Replace("]]", ">")
				.Trim()
				.Replace("\r\n", "<br />")
				.Replace("<br />", "<br />\r\n")					//
				.Replace("<ol>", "\r\n<ol>")						//
				.Replace("<ul>", "\r\n<ul>")						//	Source formatting for readability
				.Replace("</ol>", "\r\n</ol>\r\n")					//
				.Replace("</ul>", "\r\n</ul>\r\n")					//
				.Replace("<li>", "\r\n <li>") 						//
				.Replace("<a href=", "<a target=\"_blank\" href=")	// Ensure all hyperlinks go to a new window

				// Redirect all links and image requests to KConnolly.NET
				.Replace("href=\"http://www.damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://products.damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://blog.damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://www.zi255.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://zi255.com", "href=\"http://www.kconnolly.net")
				.Replace("src=\"http://www.damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://products.damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://blog.damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://www.zi255.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://zi255.com", "src=\"http://www.kconnolly.net")

				// Oops
				.Replace("Verizon Business", "A Major Telecom")
				.Replace("Verizon", "A Major Telecom");
			while (input.Contains("<br /><br /><br />")) input = input.Replace("<br /><br />", "<br />");
			return input;
		}
		/// <summary>Given the rendered and prepared blog post body saved in SQL, reformats it for editing in the Admin textbox.</summary>
		/// <param name="input">Encoded html</param>
		/// <returns>Blog post body in wonky blog post-editing syntax</returns>
		public static string DecodeForEditing(string input)
		{
			if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input");
			input = input
				.Trim()
				//.Replace("<", "[[") // HTML support
				//.Replace(">", "]]") //
				.Replace("[[", "<")
				.Replace("]]", ">")
				.Replace("<br />", "\r\n");
			while (input.Contains("\r\n\r\n\r\n")) input = input.Replace("\r\n\r\n\r\n", "\r\n\r\n");
			return input;
		}
		/// <summary>Formats a Description field for use as a META tag</summary>
		/// <param name="Desc">Unformatted Description text</param>
		/// <param name="MaxLen">Maximum length</param>
		/// <returns>META tag for insertion</returns>
		public static HtmlMeta FormatDescription(string Desc, Int16 MaxLen)
		{
			if (string.IsNullOrEmpty(Desc))
				throw new ArgumentNullException("Desc", "Cannot format null Description");

			Desc =
				System.Text.RegularExpressions.Regex.Replace(Desc.Replace("[[", "<").Replace("]]", ">"), @"<.*?>", " ")
				.Replace("<", "")
				.Replace(">", "")
				.Replace("[[", "")
				.Replace("]]", "")
				.Replace("\r", "")
				.Replace("\n", " ");
			while (Desc.Contains("  "))
				Desc = Desc.Replace("  ", " ");

			if (Desc.Length > MaxLen && MaxLen > 5) Desc = Desc.Substring(0, MaxLen - 5) + "[...]";
			else if (Desc.Length > MaxLen) Desc = Desc.Substring(0, MaxLen);

			using (HtmlMeta meta = new HtmlMeta())
			{
				meta.Name = "Description";
				meta.Content = Desc;
				return meta;
			}
		}
	}
}
